package com.ymatou.iapi.spu.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productspu.parameter.PassBean;
import com.ymatou.iapi.productspu.parameter.SPUDTO;
import com.ymatou.iapi.productspu.service.PassCall;
import com.ymatou.iapi.productspu.service.PspSpuSyncCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.sqlwapper.SpuPropertyWapper;
import com.ymttest.utils.TestDataManager;
/**
*执行同步指令
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_PspSpuSync{
	private static PspSpuSyncCall PspSpuSyncCall;
	private static SpuPropertyWapper SpuPropertyWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("执行同步指令");
	}

	@Before
	public void caseUp() {
		PspSpuSyncCall=new PspSpuSyncCall();
		SpuPropertyWapper=new SpuPropertyWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(PspSpuSyncCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(String ProductId)  throws InterruptedException {
		SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
		Map spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(ProductId);
		Integer spuId=(Integer) spuinfo.get("SpuId");
		Map pinfo=SpuPropertyWapper.selectProductInfoByProductId(ProductId);
//		Logger.debug(spuinfo);
//		Logger.debug(pinfo);
		Logger.verifyEquals(spuinfo.get("BarCode")==null?null:spuinfo.get("BarCode"),pinfo.get("BarCode"),"验证BarCode");
		Logger.verifyEquals(spuinfo.get("BrandEnName")==null?null:spuinfo.get("BrandEnName"),pinfo.get("sBrandEn"),"验证BrandEnName");
		Logger.verifyEquals(spuinfo.get("BrandId")==null?0:spuinfo.get("BrandId"),pinfo.get("iBrandId"),"验证BrandId");
		Logger.verifyEquals(spuinfo.get("BrandName")==null?null:spuinfo.get("BrandName"),pinfo.get("sBrand"),"验证BrandName");

//		Logger.verifyEquals(spuinfo.get("IsPspSpu")==null?null:spuinfo.get("IsPspSpu"),spu.getIsPspSpu(),"验证IsPspSpu");
//		Logger.verifyEquals(spuinfo.get("IsRecmd")==null?false:spuinfo.get("IsRecmd"),spu.getIsRecmd(),"验证IsRecmd");

		Logger.verifyEquals(spuinfo.get("ListingTime")==null?null:spuinfo.get("ListingTime"),pinfo.get("ListingTime"),"验证ListingTime");
		Logger.verifyEquals(spuinfo.get("MasterCategoryId")==null?null:spuinfo.get("MasterCategoryId"),pinfo.get("iMasterCategory"),"验证MasterCategoryId");
//		Logger.verifyEquals(spuinfo.get("MasterCategoryName")==null?null:spuinfo.get("MasterCategoryName"),pinfo.get("iMasterCategory"),"验证MasterCategoryName");
//		Logger.verifyEquals(spuinfo.get("CountryName")==null?null:spuinfo.get("CountryName"),spu.getOrigin(),"验证Origin");
//		Logger.verifyEquals(spuinfo.get("PriceEnd")==null?null:new BigDecimal(spuinfo.get("PriceEnd").toString()).setScale(2),spu.getPriceEnd().setScale(2),"验证PriceEnd");
//		Logger.verifyEquals(spuinfo.get("PriceStart")==null?null:new BigDecimal(spuinfo.get("PriceStart").toString()).setScale(2),spu.getPriceStart().setScale(2),"验证PriceStart");
		Logger.verifyEquals(spuinfo.get("DescText")==null?null:spuinfo.get("DescText"),pinfo.get("WordContent"),"验证ProductDescription");
		Logger.verifyEquals(spuinfo.get("ProductName")==null?null:spuinfo.get("ProductName"),pinfo.get("sProduct"),"验证ProductName");
		Logger.verifyEquals(spuinfo.get("ProductPics")==null?null:spuinfo.get("ProductPics"),pinfo.get("Pictures"),"验证ProductPics");
		Logger.verifyEquals(spuinfo.get("Summary")==null?null:spuinfo.get("Summary"),pinfo.get("Summary"),"验证ProductSummary");
		Logger.verifyEquals(spuinfo.get("SecondCategoryId")==null?null:spuinfo.get("SecondCategoryId"),pinfo.get("iCategoryId"),"验证SecondCategoryId");
//		Logger.verifyEquals(spuinfo.get("SecondCategoryName")==null?null:spuinfo.get("SecondCategoryName"),spu.getSecondCategoryName(),"验证SecondCategoryName");
//		Logger.verifyEquals(spuinfo.get("SpuId")==null?null:spuinfo.get("SpuId"),spu.getSPUId(),"验证SPUId");
		Logger.verifyEquals(spuinfo.get("ThirdCategoryId")==null?null:spuinfo.get("ThirdCategoryId"),pinfo.get("iThirdCategoryId"),"验证ThirdCategoryId");
//		Logger.verifyEquals(spuinfo.get("ThirdCategoryName")==null?null:spuinfo.get("ThirdCategoryName"),spu.getThirdCategoryName(),"验证ThirdCategoryName");
		
		Logger.verifyEquals(spuinfo.get("Unit")==null?null:spuinfo.get("Unit"),pinfo.get("Unit"),"验证Unit");
		Logger.verifyEquals(spuinfo.get("Weight")==null?null:spuinfo.get("Weight"),pinfo.get("Weight"),"验证Weight");
		Logger.verifyEquals(spuinfo.get("Width")==null?null:spuinfo.get("Width"),pinfo.get("Width"),"验证Width");
		Logger.verifyEquals(spuinfo.get("Height")==null?null:spuinfo.get("Height"),pinfo.get("Height"),"验证Height");
		Logger.verifyEquals(spuinfo.get("Length")==null?null:spuinfo.get("Length"),pinfo.get("Length"),"验证Length");
//		
//		List<String> pp = spu.getProductPictures();
//		Collections.sort(pp);
//		List<String> pp2=(List<String>)( spuinfo.get("ProductPics")==null?new ArrayList<>():Arrays.asList(spuinfo.get("ProductPics").toString().split(",")));
//		Collections.sort(pp2);
//		Logger.verifyEquals(pp2.toString(),pp.toString(),"验证ProductPictures");
//		
		List<String> pdp = (List<String>)(pinfo.get("DescPics")==null?new ArrayList<>():Arrays.asList(pinfo.get("DescPics").toString().split(",")));
		Collections.sort(pdp);
		
		List<String> pdp2=(List<String>)( spuinfo.get("DescPics")==null?new ArrayList<>():Arrays.asList(spuinfo.get("DescPics").toString().split(",")));
		Collections.sort(pdp2);
		Logger.verifyEquals(pdp2.toString(),pdp.toString(),"验证ProductDescriptionPics");
//		
		List<Map> pinfo2 = SpuPropertyWapper.selectSpuPropertyInfosBySpuid(spuId);
		List<Map> spinfo=SpuPropertyWapper.selectProductPropertyByProductId(ProductId);
		Logger.debug(pinfo2);
		Logger.debug(spinfo);
		Map<String,Map> mpinfo=new HashMap<>();
		pinfo2.forEach(m->mpinfo.put(m.get("Values").toString(),m));
		Logger.verifyEquals(pinfo2.size(), spinfo.size(), "验证PropertyPairs的size");
		spinfo.forEach(pps->{
			Map mpp = mpinfo.get(pps.get("Value"));
			Logger.verifyEquals(mpp.get("Values")==null?null:mpp.get("Values").toString(), pps.get("Value"), "验证Propert.yName");
			Logger.verifyEquals(mpp.get("PropertyId")==null?null:mpp.get("PropertyId"), pps.get("PropertyId"), "验证PropertyId");
			Logger.verifyEquals(mpp.get("Status")==null?null:mpp.get("Status"), pps.get("Status"), "验证PropertyStatus");
		});
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PspSpuSync_001() {
		Logger.start(true,"执行同步指令成功");
		try {
//			ProductManagerCallServiceV2.offSaleAllProduct(3383);
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro.getProduct().setProductName(TestDataManager.getChineseName()+TestDataManager.getRandomUUID());
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
//			Map spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(ProductId);
//			Logger.debug(spuinfo);
//			Integer spuId=(Integer) spuinfo.get("SpuId");
//			PassBean passBean=new PassBean();
//			PassCall passCall=new PassCall();
//			passBean.setAuditor("autoTest");
//			passBean.setSPUId(spuId);
//			passCall.setData(passBean);
//			passCall.callService();			
//			Logger.verifyEquals("200", passCall.getString("Status"), "验证Status");
			
			PspSpuSyncCall.callService();
			Logger.verifyIsContains("ok", PspSpuSyncCall.getReturnData(), "验证返回");
//			SQL
//			SELECT sProductId FROM dbo.Ymt_Products with(nolock) WHERE iAction >-1 AND iUserId = @UserId
//			3）发送总线
//
//			参考：API-3.执行同步指令
//
//			总线配置
//			AppId	product
//			Code	syncpspspu
//			MessageId	productId
//			消息体	{ "ProductId":  productId}
//			订阅者	http://productspu.iapi.ymatou.com/api/Spu/SyncPspSpuProduct 
			verify(ProductId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PspSpuSync_002() {
		Logger.start(true,"指定pid执行同步指令成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro.getProduct().setProductName(TestDataManager.getChineseName()+TestDataManager.getRandomUUID());
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
			PspSpuSyncCall.setData("ProductId="+ProductId);
			PspSpuSyncCall.callService();
			Logger.verifyIsContains("ok", PspSpuSyncCall.getReturnData(), "验证返回");
			verify(ProductId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PspSpuSync_003() {
		Logger.start(true,"多次执行指定pid执行同步指令成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro.getProduct().setProductName(TestDataManager.getChineseName()+TestDataManager.getRandomUUID());
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
			PspSpuSyncCall.setData("ProductId="+ProductId);
			PspSpuSyncCall.callService();
			Logger.verifyIsContains("ok", PspSpuSyncCall.getReturnData(), "验证返回");
			
			
			AddSellerProductRequest addpro2 = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro2.getProduct().setProductName(TestDataManager.getChineseName()+TestDataManager.getRandomUUID());
			String ProductId2 = ProductManagerCallServiceV2.addNewProduct(addpro2).get("Data").getAsJsonObject().get("ProductId").getAsString();
			PspSpuSyncCall.setData("ProductId="+ProductId2);
			PspSpuSyncCall.callService();
			Logger.verifyIsContains("ok", PspSpuSyncCall.getReturnData(), "验证返回");
			
			verify(ProductId);
			verify(ProductId2);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verify(String productId) throws ClientProtocolException, IOException, InterruptedException {
		Logger.comment("验证总线发送消息");
		MessageListBean MessageListBean = MqQueryCallService.initPspSpuSync(productId);
		List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
		int redo=0;
		while(joa.size()==0&&redo<5){
			redo++;
			Thread.sleep(5000);
			 joa = MqQueryCallService.GetMqMessage(MessageListBean);
		}
		Logger.verifyEquals(1, joa.size(), "验证发送总线消息");
		Logger.verifyEquals(productId,joa.get(0).get("productId").toString(), "验证ProductId");
		Thread.sleep(5000);
		Map spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(productId);
		Integer status = (Integer) spuinfo.get("Status");
		Logger.verifyEquals(-2, status, "验证status为-2");
	}

}